package com.the8thfold.anton.utils

import com.the8thfold.anton.settings.Configuration
import com.the8thfold.anton.settings.ConfigurationItem
import com.the8thfold.anton.ant.AntJobRunResults
import com.the8thfold.anton.ant.AntScript
import com.the8thfold.anton.ant.AntJob

/**
 * Created by IntelliJ IDEA.
 * User: home
 * Date: 14/05/11
 * Time: 08:33
 * To change this template use File | Settings | File Templates.
 */
class ConfigurationUtils {

    public static String getOutputPath() {
        File outputPathFile = new File(getConfigurationItem("outputPath"));
        if (!outputPathFile.exists()) {
            outputPathFile.mkdirs();
        }
        return outputPathFile.absolutePath;
    }

    public static String getScriptsFolderName() {
        File scriptsFolder = new File(getOutputPath(),getConfigurationItem("scriptsFolderName"));
        if (!scriptsFolder.exists()) {
            scriptsFolder.mkdirs();
        }
        return scriptsFolder.absolutePath;
    }

    public static String getScriptFolderName(AntScript script) {
        File scriptsFolder = new File(getScriptsFolderName());
        File scriptFolder = new File(scriptsFolder,script.scriptId)
        if (!scriptFolder.exists()) {
            scriptFolder.mkdirs();
        }
        return scriptFolder.absolutePath;
    }

    public static String getJobFolderName(AntJob job) {
        File scriptFolder = new File(getScriptFolderName(job.script));
        File jobsFolder = new File(scriptFolder, getConfigurationItem("jobsFolderName"))
        File jobFolder = new File(jobsFolder, job.jobId)
        if (!jobFolder.exists()) {
            jobFolder.mkdirs();
        }
        return jobFolder.absolutePath;
    }

    public static String getResultFolderName(AntJobRunResults result) {
        File jobFolder = new File(getJobFolderName(result.job))
        File resultsFolder = new File(jobFolder, getConfigurationItem("resultsFolderName"))
        File resultFolder = new File(resultsFolder, result.runResultsId)
        if (!resultFolder.exists()) {
            resultFolder.mkdirs();
        }
        return resultFolder.absolutePath;
    }


    public static String getConfigurationItem(String item) {
        ConfigurationItem.findByKey(item)?.value
    }
}
